<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=0, initial-scale=1.0" />
    <title>迭代器</title>
  </head>
  <body>
    <script>
      // 声明一个数组
      const arr = ["A", "B", "C", "D"];

      // 使用 for....of 遍历数组
      for (let item of arr) {
        // 保存数组中的键值
        console.log(item);
      }

      // 使用 for...in 遍历数组
      for (let i in arr) {
        // 保存数组的键名(下标)
        console.log(i);
      }

      console.log(arr); // 存在Symbol(Symbol.iterator)
      let iterator = arr[Symbol.iterator]();
      console.log(iterator); // 存在 next()方法
      console.log(iterator.next()); //{value: 'A', done: false}
      console.log(iterator.next());
      console.log(iterator.next());
      console.log(iterator.next());
      console.log(iterator.next()); //{value: undefined, done: true}
    </script>
  </body>
</html>
